//
//  FuJinViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "FuJinViewController.h"
#import "FuJin.h"
#import "FuJiSmallCollectionViewCell.h"
#import "FuJinBigCollectionViewCell.h"
#import "FuJinErViewController.h"
@interface FuJinViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, FuJinBigCollectionViewCellDelegate>

@property (nonatomic, retain) NSMutableArray *smallArr;
@property (nonatomic, retain) BaseCollectionView *bigCollection;
@property (nonatomic, retain) BaseCollectionView *SmallCollection;
@property (nonatomic, retain) UIView *view1;




@end

@implementation FuJinViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_smallArr  release];
    [_SmallCollection release];
    [_bigCollection release];
    [_view1 release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.9 green:0.29 blue:0.44 alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = UIColorFromRGB(0xF0F0F0);
    
    self.smallArr = [NSMutableArray arrayWithObjects:@"全部", @"景点", @"住宿",@"餐厅",@"休闲娱乐",@"购物",@"", nil];
    
   // NSString *str = _location[@"lat"];
    
    if (_name == nil) {
        self.navigationItem.title = @"我的附近";
    }else{
    self.navigationItem.title = [NSString stringWithFormat:@"%@的附近", _name];
    }
    
    
    [self createBigCollection];
    
    
    [self createSmallCollection];
    
    [self createView];
    
}

- (void)setName:(NSString *)name{
    if (_name != name) {
        [_name release];
        _name = [name retain];
    }
}

/**<  完成协议方法 */

- (void)jumpToVc:(FuJin *)model{
  
    FuJinErViewController *er = [[FuJinErViewController alloc] init];
    er.model = model;
    [self.navigationController pushViewController:er animated:YES];
    
    [er release];
}

- (void)setLocation:(NSDictionary *)location{
    if (_location != location) {
        [_location release];
        _location = [location retain];
    }
}

- (void)createView{
    
    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(8, 40, 40, 3)];
    
    _view1.backgroundColor = UIColorFromRGB(0x099cc);
    
    [self.view addSubview:_view1];
    
    
    [_view1 release];
    
    
}

- (void)createBigCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.bigCollection = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0,50, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    
    _bigCollection.delegate = self;
    _bigCollection.dataSource = self;
    
    _bigCollection.pagingEnabled = YES;
    _bigCollection.bounces = YES;
    [self.view addSubview:_bigCollection];
    [_bigCollection release];
    
    
    [_bigCollection registerClass:[FuJinBigCollectionViewCell class] forCellWithReuseIdentifier:@"FuJinBigCollectionViewCell"];
    
}

- (void)createSmallCollection{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5,5);
    
    
    self.SmallCollection = [[BaseCollectionView  alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) collectionViewLayout:layout];
    _SmallCollection.delegate = self;
    _SmallCollection.dataSource = self;
    _SmallCollection.userInteractionEnabled = NO;
    [self.view addSubview:_SmallCollection];
    [_SmallCollection release];
    
    [_SmallCollection registerClass:[FuJiSmallCollectionViewCell class] forCellWithReuseIdentifier:@"FuJiSmallCollectionViewCell"];
    
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSInteger a = _bigCollection.contentOffset.x / self.view.frame.size.width;
    
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"yeJianMoShi"] isEqualToString:@"isDay"]) {
        [UIView animateWithDuration:0.3 animations:^{
            
            if (a == 4) {
                _view1.frame = CGRectMake(5 + a*(16 + 30) , 40, 80, 3);
            }else if (a == 5){
                _view1.frame = CGRectMake(5 + a*(16 + 30)+38, 40, 40, 3);
                
            }else{
                _view1.frame = CGRectMake(5 + a*(16 + 30), 40, 40, 3);
            }
            
            FuJiSmallCollectionViewCell *cell = [_SmallCollection viewWithTag:1000 + a];
            
            cell.label.textColor = UIColorFromRGB(0x099cc);
            cell.label.font = [UIFont boldSystemFontOfSize:18];
            
            FuJiSmallCollectionViewCell *cell1 = [_SmallCollection viewWithTag:1000 + a - 1];
            cell1.label.textColor = [UIColor blackColor];
            cell1.label.font = [UIFont boldSystemFontOfSize:15];
            
            FuJiSmallCollectionViewCell *cell2 = [_SmallCollection viewWithTag:1000 + a + 1];
            cell2.label.textColor = [UIColor blackColor];
            cell2.label.font = [UIFont boldSystemFontOfSize:15];
            
        }];
        
    }else{
        [UIView animateWithDuration:0.3 animations:^{
            
            if (a == 4) {
                _view1.frame = CGRectMake(5 + a*(16 + 30) , 110, 80, 3);
            }else if (a == 5){
                _view1.frame = CGRectMake(5 + a*(16 + 30)+38, 110, 40, 3);
                
            }else{
                _view1.frame = CGRectMake(5 + a*(16 + 30), 110, 40, 3);
            }
            
            FuJiSmallCollectionViewCell *cell = [_SmallCollection viewWithTag:1000 + a];
            
            cell.label.textColor = UIColorFromRGB(0x099cc);
            cell.label.font = [UIFont boldSystemFontOfSize:18];
            
            FuJiSmallCollectionViewCell *cell1 = [_SmallCollection viewWithTag:1000 + a - 1];
            cell1.label.textColor = [UIColor whiteColor];
            cell1.label.font = [UIFont boldSystemFontOfSize:15];
            
            FuJiSmallCollectionViewCell *cell2 = [_SmallCollection viewWithTag:1000 + a + 1];
            cell2.label.textColor = [UIColor whiteColor];
            cell2.label.font = [UIFont boldSystemFontOfSize:15];
            
        }];

    }
    
    
    
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _SmallCollection) {
        
        if (indexPath.row == 4) {
            return CGSizeMake(80, 30);
        }else{
            return CGSizeMake(40, 30);
        }
    }else{
        return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView == _SmallCollection) {
        return _smallArr.count;
    }else{
        
        return 6;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == _SmallCollection) {
        FuJiSmallCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FuJiSmallCollectionViewCell" forIndexPath:indexPath];
        cell.backgroundColor = [UIColor clearColor];
        cell.label.text = _smallArr[indexPath.row];
        if (indexPath.row == 0) {
            cell.label.font = [UIFont systemFontOfSize:19];
            cell.label.textColor = UIColorFromRGB(0x099cc);
            cell.tag = 1000 + indexPath.row;
        }
        cell.tag = 1000 + indexPath.row;
        return cell;
        
    }else{
        
        
        FuJinBigCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"FuJinBigCollectionViewCell" forIndexPath:indexPath];
        
        if (_location != nil) {
            cell.location = _location;
        }else{

            NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@38.883521, @"lat", @121.544549, @"lng", nil];
            
            cell.location = dic;
            
            
            
        }
        cell.delegate = self;
       
        if (indexPath.row == 0) {
            cell.url = 0;
            return cell;
            
            
        }else if (indexPath.row == 1){
            
            
            cell.url = 11;
            return cell;
        }else if (indexPath.row == 2){
            
            cell.url = 10;
            return cell;
        }else if (indexPath.row == 3){
            
            cell.url = 5;
            return cell;
        }else if (indexPath.row == 4){
            
            cell.url = 21;
            return cell;
        }else{
             
            cell.url = 6;
            return cell;
        }
        
        
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
   
    cell.layer.transform = CATransform3DMakeRotation(2, 0, 1, 0);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeRotation(0, 0, 0, 0);
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
